package net.sxnic.ugr.resource.util;

import java.io.File;
import java.util.List;

import net.sxnic.ugr.resource.Resource;
import net.sxnic.ugr.resource.ResourceManager;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ResourceUtils {
	
	private static Logger logger = LoggerFactory.getLogger(ResourceUtils.class);
	
	public static void readResourceFromFile(File file,ResourceManager resManager) throws Exception{
		
		if(file ==null){
			return;
		}
		
		List<String> list = FileUtils.readLines(file,"UTF-8");
		
		
		String[] r_s = null;
		Resource res = null;
		for(String line:list){
			if(StringUtils.isBlank(line) || line.startsWith("#")){
				continue;
			}
			
			r_s = StringUtils.split(line.trim(), ",");
			
			res = resManager.findByUnique("code", r_s[1]);
			
			if(res ==null){
				res = new Resource();
			}
			
			res.setModule(r_s[0]);
			res.setCode(r_s[1]);
			res.setName(r_s[2]);
			res.setNamespace(r_s[3]);
			
			resManager.save(res);			
			
			logger.debug("===资源信息更新成功==="+res.getCode());
		}
		
	}

}
